package com.example.audiotunning;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.method.ScrollingMovementMethod;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;


enum FilterType{
    Peak,
    Lowpass,
    Highpass,
    Treble_Shelf,
    Bass_Shelf,
    Notch,
}

enum SendType{
NoneSend,

Filter_1,

Filter_2,

Filter_3,

Filter_4,

Filter_5,

Filter_6,

Filter_7,

Filter_8,

Filter_9,

Filter_10,

Filter_11,

Filter_12,

Filter_13,

Filter_14,

Filter_15,

CHPara,//gain delay polarity mute

Loudness,

Limiter;



}
class EqData{
    public int m_type;
    public final int m_order = 2;
    public int m_fc;
    public float m_gain;
    public  float m_q;
    public  boolean m_selected;
    public EqData(int type, int fc, float gain, float q, boolean b ){
        this.m_selected = b;
        this.m_fc = fc;
        this.m_type = type;
        this.m_gain = gain;
        this.m_q = q;

    }
}

class ChParas{
    public int m_mute;
    public int m_polarity;
    public float m_delay;
    public float m_gain;
    public ChParas(){
        this.m_mute = 0;
        this.m_polarity = 0;
        this.m_delay = 0;
        this.m_gain = 0;
    }
    public ChParas(int mute, int polarity, float delay, float gain){
        this.m_mute = mute;
        this.m_polarity = polarity;
        this.m_delay = delay;
        this.m_gain = gain;
    }
}

@SuppressLint("NotConstructor")
class AlgPara
{
    static final int  MAX_BANDS  = 15;
    static final int  BTM_BANDS	= 9;
    static final int  LOUDNESS_BANDS = 2;
    static final int [] FREQ_LIST = {32, 63, 125, 250, 315, 500, 850, 1000, 1400, 1600, 2000, 2500, 3150, 4000, 5000};
    public EqData[] BMT_params;
    public EqData[] Loudness_params;
    public EqData[] GEQ_FL_params;
    public EqData[] GEQ_FR_params;
    public EqData[] GEQ_RL_params;
    public EqData[] GEQ_RR_params;
    public ChParas CH_FL_paras;
    public ChParas CH_RL_paras;
    public ChParas CH_FR_paras;
    public ChParas CH_RR_paras;


    public  AlgPara(){
        int fc=0, type = 0;
        float gain=0.0f, q= 1.414f;
        boolean b = false;
        BMT_params = new EqData[BTM_BANDS];
        Loudness_params= new EqData[LOUDNESS_BANDS];
        GEQ_FL_params= new EqData[MAX_BANDS];
        GEQ_FR_params= new EqData[MAX_BANDS];
        GEQ_RL_params= new EqData[MAX_BANDS];
        GEQ_RR_params= new EqData[MAX_BANDS];
        CH_FL_paras = new ChParas();
        CH_FR_paras = new ChParas();
        CH_RL_paras = new ChParas();
        CH_RR_paras = new ChParas();
        for (int i=0; i<MAX_BANDS; i++){

            fc = FREQ_LIST[i];
            type = FilterType.Peak.ordinal();
            gain = 0f;
            q = 1.414f;
            b = false;
            GEQ_FL_params[i] = new EqData(type, fc, gain, q, b);
        }
        for (int i=0; i<MAX_BANDS; i++){
            fc = FREQ_LIST[i];
            type = FilterType.Peak.ordinal();
            gain = 0f;
            q = 1.414f;
            b = false;
            GEQ_FR_params[i] = new EqData(type, fc, gain, q, b);
        }
        for (int i=0; i<MAX_BANDS; i++){
            fc = FREQ_LIST[i];
            type = FilterType.Peak.ordinal();
            gain = 0f;
            q = 1.414f;
            b = false;
            GEQ_RL_params[i] = new EqData(type, fc, gain, q, b);
        }
        for (int i=0; i<MAX_BANDS; i++){
            fc = FREQ_LIST[i];
            type = FilterType.Peak.ordinal();
            gain = 0f;
            q = 1.414f;
            b = false;
            GEQ_RR_params[i] = new EqData(type, fc, gain, q, b);
        }
    }
};

public  class MainActivity extends AppCompatActivity implements UdpReceiverListener {

    private final int IP_PORT = 8080;
    private final int header_val = 0x20250619;
    private final int MAX_LINES = 200;
    private final int MAX_SPEAKERS = 4;
    private String ip_addr = "192.168.1.1";
    private String local_ip = "192.168.1.2";
    private Switch btn_connect_server;
    private int cur_channel = 0;
    private AlgPara a_para;
    private SpannableStringBuilder textBuilder;
    private List<Spinner> sp_list;
    private List<EditText> ed_q_list;

    private List<EditText> ed_gain_list;

    private List<EditText> ed_freq_list;

    private List<CheckBox> cb_bypass_list;


    private EditText ed_ch_gain;
    private EditText ed_ch_delay;
    private CheckBox cb_ch_polarity;
    private  CheckBox cb_ch_mute;

    private TextView text_log;

    private UdpCommunication client;

    private SendType globalSendType = SendType.NoneSend;


    boolean is_connected = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        initParam();
        initView();
    }

    @Override
    public void onDataReceived(byte[] data, String senderIp, int senderPort) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showInfoToTextLogView(data, senderIp, senderPort, 1);
            }
        });
    }

    @Override
    public void onError(String errorMsg) {
        Toast.makeText(MainActivity.this, "UDP error：" + errorMsg, Toast.LENGTH_SHORT).show();
    }

    private void showInfoToTextLogView(byte[] data, String senderIp, int senderPort, int type)
    {
        LocalDateTime now = LocalDateTime.now();
        StringBuilder str = new StringBuilder();
        str.append("\n[");
        str.append(now.getHour()+":");
        str.append(now.getMinute()+":");
        str.append(now.getSecond()+":");
        str.append(now.getNano()/1000000);
        str.append("] <");
        str.append(senderIp);
        str.append(" ");
        str.append(String.valueOf(senderPort));

        //Send data
        if (type == 0)
        {
            str.append("> SEND/");
            str.append(String.valueOf(data.length));
            str.append("\n");

            for (byte d:data){
                str.append(String.format("%02X ", d));
            }


        } else if (type == 1) {//recv data
            str.append("> RECV/");
            str.append(String.valueOf(data.length));
            str.append("\n");

            String d = new String(data);
            str.append(d);
        }
        else {
            return;
        }
        textBuilder.append(str.toString());
        // 按行分割文本，只保留最后100行
        String[] allLines = textBuilder.toString().split("\n");
        if (allLines.length > MAX_LINES) {
            // 计算需要保留的起始行索引
            int startIndex = allLines.length - MAX_LINES;
            SpannableStringBuilder newBuilder = new SpannableStringBuilder();

            // 重新拼接最后100行
            for (int i = startIndex; i < allLines.length; i++) {
                newBuilder.append(allLines[i]);
                if (i < allLines.length - 1) {
                    newBuilder.append("\n"); // 保留换行符
                }
            }

            // 更新文本构建器
            textBuilder = newBuilder;
        }

        // 更新TextView并滚动到最新位置
        text_log.setText(textBuilder);


        ScrollView scrollView = findViewById(R.id.sv_log);
        //scrollView.post(() -> scrollView.smoothScrollTo(0, text_log.getBottom()));
        scrollView.post(() -> {
            scrollView.scrollTo(0, scrollView.getChildAt(0).getHeight());
        });
//       text_log.append(str.toString());

    }
    private void initParam(){
        a_para = new AlgPara();
    }

    private boolean isIpAddressValid(String ip) {
        if (ip == null || ip.trim().isEmpty()) {
            return false;
        }
        String pattern = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$";
        if (!ip.matches(pattern)) {
            return false;
        }
        String[] segments = ip.split("\\.");
        for (String segment : segments) {
            int value = Integer.parseInt(segment);
            if (value < 0 || value > 255) {
                return false;
            }
        }
        return true;
    }
    private void connectToServer(){
        ip_addr = "";
        EditText et = findViewById(R.id.edit_ip_1);
        ip_addr += et.getText().toString();
        ip_addr += ".";

        et = findViewById(R.id.edit_ip_2);
        ip_addr += et.getText().toString();
        ip_addr += ".";

        et = findViewById(R.id.edit_ip_3);
        ip_addr += et.getText().toString();
        ip_addr += ".";

        et = findViewById(R.id.edit_ip_4);
        ip_addr += et.getText().toString();


        if (isIpAddressValid(ip_addr)){
            client = new UdpCommunication(local_ip, IP_PORT);
            client.setUdpReceiverListener(MainActivity.this);
            client.startReceiving();

        }
    }
    private void disconnectFromServer() {
        if (client != null){
            client.close();

        }
    }

    private void saveData(int channel)
    {
        float gain = Float.parseFloat(ed_ch_gain.getText().toString());
        float delayms = Float.parseFloat(ed_ch_delay.getText().toString());
        int polarity = cb_ch_polarity.isChecked()?1:0;
        int mute = cb_ch_mute.isChecked()?1:0;
        if (channel==0){
            for (int i=0; i<sp_list.size(); i++)
            {
                a_para.GEQ_FL_params[i].m_type = sp_list.get(i).getSelectedItemPosition();
            }

            for (int i=0; i<ed_q_list.size(); i++)
            {
                a_para.GEQ_FL_params[i].m_q = Float.parseFloat(ed_q_list.get(i).getText().toString());
            }
            for (int i=0; i<ed_gain_list.size(); i++)
            {
                a_para.GEQ_FL_params[i].m_gain = Float.parseFloat(ed_gain_list.get(i).getText().toString());
            }
            for (int i=0; i<ed_freq_list.size(); i++)
            {
                a_para.GEQ_FL_params[i].m_fc = Integer.parseInt(ed_freq_list.get(i).getText().toString());
            }

            for (int i=0; i<cb_bypass_list.size(); i++)
            {
                a_para.GEQ_FL_params[i].m_selected = cb_bypass_list.get(i).isChecked();
            }

            a_para.CH_FL_paras.m_gain = gain;
            a_para.CH_FL_paras.m_polarity = polarity;
            a_para.CH_FL_paras.m_delay = delayms;
            a_para.CH_FL_paras.m_mute = mute;

        } else if (channel==1) {
            for (int i = 0; i < sp_list.size(); i++) {
                a_para.GEQ_FR_params[i].m_type = sp_list.get(i).getSelectedItemPosition();
            }

            for (int i = 0; i < ed_q_list.size(); i++) {
                a_para.GEQ_FR_params[i].m_q = Float.parseFloat(ed_q_list.get(i).getText().toString());
            }
            for (int i = 0; i < ed_gain_list.size(); i++) {
                a_para.GEQ_FR_params[i].m_gain = Float.parseFloat(ed_gain_list.get(i).getText().toString());
            }
            for (int i = 0; i < ed_freq_list.size(); i++) {
                a_para.GEQ_FR_params[i].m_fc = Integer.parseInt(ed_freq_list.get(i).getText().toString());
            }

            for (int i = 0; i < cb_bypass_list.size(); i++) {
                a_para.GEQ_FR_params[i].m_selected = cb_bypass_list.get(i).isChecked();
            }
            a_para.CH_FR_paras.m_gain = gain;
            a_para.CH_FR_paras.m_polarity = polarity;
            a_para.CH_FR_paras.m_delay = delayms;
            a_para.CH_FR_paras.m_mute = mute;
        }else if (channel==2) {
            for (int i=0; i<sp_list.size(); i++)
            {
                a_para.GEQ_RL_params[i].m_type = sp_list.get(i).getSelectedItemPosition();
            }

            for (int i=0; i<ed_q_list.size(); i++)
            {
                a_para.GEQ_RL_params[i].m_q = Float.parseFloat(ed_q_list.get(i).getText().toString());
            }
            for (int i=0; i<ed_gain_list.size(); i++)
            {
                a_para.GEQ_RL_params[i].m_gain = Float.parseFloat(ed_gain_list.get(i).getText().toString());
            }
            for (int i=0; i<ed_freq_list.size(); i++)
            {
                a_para.GEQ_RL_params[i].m_fc = Integer.parseInt(ed_freq_list.get(i).getText().toString());
            }

            for (int i=0; i<cb_bypass_list.size(); i++)
            {
                a_para.GEQ_RL_params[i].m_selected = cb_bypass_list.get(i).isChecked();
            }
            a_para.CH_RL_paras.m_gain = gain;
            a_para.CH_RL_paras.m_polarity = polarity;
            a_para.CH_RL_paras.m_delay = delayms;
            a_para.CH_RL_paras.m_mute = mute;
        }else if (channel==3) {
            for (int i=0; i<sp_list.size(); i++)
            {
                a_para.GEQ_RR_params[i].m_type = sp_list.get(i).getSelectedItemPosition();
            }

            for (int i=0; i<ed_q_list.size(); i++)
            {
                a_para.GEQ_RR_params[i].m_q = Float.parseFloat(ed_q_list.get(i).getText().toString());
            }
            for (int i=0; i<ed_gain_list.size(); i++)
            {
                a_para.GEQ_RR_params[i].m_gain = Float.parseFloat(ed_gain_list.get(i).getText().toString());
            }
            for (int i=0; i<ed_freq_list.size(); i++)
            {
                a_para.GEQ_RR_params[i].m_fc = Integer.parseInt(ed_freq_list.get(i).getText().toString());
            }

            for (int i=0; i<cb_bypass_list.size(); i++)
            {
                a_para.GEQ_RR_params[i].m_selected = cb_bypass_list.get(i).isChecked();
            }
            a_para.CH_RR_paras.m_gain = gain;
            a_para.CH_RR_paras.m_polarity = polarity;
            a_para.CH_RR_paras.m_delay = delayms;
            a_para.CH_RR_paras.m_mute = mute;
        }
    }

    private void refreshData(int channel){
        if (channel==0){
            for (int i=0; i<sp_list.size(); i++)
            {
                sp_list.get(i).setSelection(a_para.GEQ_FL_params[i].m_type);
            }

            for (int i=0; i<ed_q_list.size(); i++)
            {
                ed_q_list.get(i).setText(String.valueOf(a_para.GEQ_FL_params[i].m_q));
            }
            for (int i=0; i<ed_gain_list.size(); i++)
            {
                ed_gain_list.get(i).setText(String.valueOf(a_para.GEQ_FL_params[i].m_gain));
            }
            for (int i=0; i<ed_freq_list.size(); i++)
            {
                ed_freq_list.get(i).setText(String.valueOf(a_para.GEQ_FL_params[i].m_fc));
            }

            for (int i=0; i<cb_bypass_list.size(); i++)
            {
                cb_bypass_list.get(i).setChecked(a_para.GEQ_FL_params[i].m_selected);
            }

            ed_ch_gain.setText(String.valueOf(a_para.CH_FL_paras.m_gain));
            ed_ch_delay.setText(String.valueOf(a_para.CH_FL_paras.m_delay));
            cb_ch_mute.setChecked(a_para.CH_FL_paras.m_mute==1);
            cb_ch_polarity.setChecked(a_para.CH_FL_paras.m_polarity==1);
        } else if (channel==1) {
            for (int i=0; i<sp_list.size(); i++)
            {
                sp_list.get(i).setSelection(a_para.GEQ_FR_params[i].m_type);
            }

            for (int i=0; i<ed_q_list.size(); i++)
            {
                ed_q_list.get(i).setText(String.valueOf(a_para.GEQ_FR_params[i].m_q));
            }
            for (int i=0; i<ed_gain_list.size(); i++)
            {
                ed_gain_list.get(i).setText(String.valueOf(a_para.GEQ_FR_params[i].m_gain));
            }
            for (int i=0; i<ed_freq_list.size(); i++)
            {
                ed_freq_list.get(i).setText(String.valueOf(a_para.GEQ_FR_params[i].m_fc));
            }

            for (int i=0; i<cb_bypass_list.size(); i++)
            {
                cb_bypass_list.get(i).setChecked(a_para.GEQ_FR_params[i].m_selected);
            }

            ed_ch_gain.setText(String.valueOf(a_para.CH_FR_paras.m_gain));
            ed_ch_delay.setText(String.valueOf(a_para.CH_FR_paras.m_delay));
            cb_ch_mute.setChecked(a_para.CH_FR_paras.m_mute==1);
            cb_ch_polarity.setChecked(a_para.CH_FR_paras.m_polarity==1);
        }else if (channel==2) {
            for (int i=0; i<sp_list.size(); i++)
            {
                sp_list.get(i).setSelection(a_para.GEQ_RL_params[i].m_type);
            }

            for (int i=0; i<ed_q_list.size(); i++)
            {
                ed_q_list.get(i).setText(String.valueOf(a_para.GEQ_RL_params[i].m_q));
            }
            for (int i=0; i<ed_gain_list.size(); i++)
            {
                ed_gain_list.get(i).setText(String.valueOf(a_para.GEQ_RL_params[i].m_gain));
            }
            for (int i=0; i<ed_freq_list.size(); i++)
            {
                ed_freq_list.get(i).setText(String.valueOf(a_para.GEQ_RL_params[i].m_fc));
            }

            for (int i=0; i<cb_bypass_list.size(); i++)
            {
                cb_bypass_list.get(i).setChecked(a_para.GEQ_RL_params[i].m_selected);
            }

            ed_ch_gain.setText(String.valueOf(a_para.CH_RL_paras.m_gain));
            ed_ch_delay.setText(String.valueOf(a_para.CH_RL_paras.m_delay));
            cb_ch_mute.setChecked(a_para.CH_RL_paras.m_mute==1);
            cb_ch_polarity.setChecked(a_para.CH_RL_paras.m_polarity==1);
        }else if (channel==3) {
            for (int i=0; i<sp_list.size(); i++)
            {
                sp_list.get(i).setSelection(a_para.GEQ_RR_params[i].m_type);
            }

            for (int i=0; i<ed_q_list.size(); i++)
            {
                ed_q_list.get(i).setText(String.valueOf(a_para.GEQ_RR_params[i].m_q));
            }
            for (int i=0; i<ed_gain_list.size(); i++)
            {
                ed_gain_list.get(i).setText(String.valueOf(a_para.GEQ_RR_params[i].m_gain));
            }
            for (int i=0; i<ed_freq_list.size(); i++)
            {
                ed_freq_list.get(i).setText(String.valueOf(a_para.GEQ_RR_params[i].m_fc));
            }

            for (int i=0; i<cb_bypass_list.size(); i++)
            {
                cb_bypass_list.get(i).setChecked(a_para.GEQ_RR_params[i].m_selected);
            }

            ed_ch_gain.setText(String.valueOf(a_para.CH_RR_paras.m_gain));
            ed_ch_delay.setText(String.valueOf(a_para.CH_RR_paras.m_delay));
            cb_ch_mute.setChecked(a_para.CH_RR_paras.m_mute==1);
            cb_ch_polarity.setChecked(a_para.CH_RR_paras.m_polarity==1);
        }
    }
    private void initSpinner() {
        String [] items = new String[]{"Peak", "LowPass", "HighPass", "HighShelf", "LowShelf", "Notch"};
        //String [] items = new String[]{"0", "1", "2", "3", "4", "5"};
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.custom_spinner_item, items);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);




        sp_list = new ArrayList<>();
        Spinner sp = findViewById(R.id.sp_type_1);
        sp_list.add(sp);

        sp = findViewById(R.id.sp_type_2);
        sp_list.add(sp);

        sp = findViewById(R.id.sp_type_3);
        sp_list.add(sp);

        sp = findViewById(R.id.sp_type_4);
        sp_list.add(sp);

        sp = findViewById(R.id.sp_type_5);
        sp_list.add(sp);

        sp = findViewById(R.id.sp_type_6);
        sp_list.add(sp);

        sp = findViewById(R.id.sp_type_7);
        sp_list.add(sp);

        sp = findViewById(R.id.sp_type_8);
        sp_list.add(sp);

        sp = findViewById(R.id.sp_type_9);
        sp_list.add(sp);

        sp = findViewById(R.id.sp_type_10);
        sp_list.add(sp);

        sp = findViewById(R.id.sp_type_11);
        sp_list.add(sp);

        sp = findViewById(R.id.sp_type_12);
        sp_list.add(sp);

        sp = findViewById(R.id.sp_type_13);
        sp_list.add(sp);

        sp = findViewById(R.id.sp_type_14);
        sp_list.add(sp);

        sp = findViewById(R.id.sp_type_15);
        sp_list.add(sp);
        for (int i=0; i<sp_list.size(); i++)
        {
            sp_list.get(i).setAdapter(adapter);
            sp_list.get(i).setSelection(0);
            TextView tv = (TextView) (sp_list.get(i).getSelectedView());
            if(tv != null){
                tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 8);
            }

            sp_list.get(i).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    int id = adapterView.getId();
                    if (id == R.id.sp_type_1) {
                        globalSendType = SendType.Filter_1;
                    } else if (id == R.id.sp_type_2) {
                        globalSendType = SendType.Filter_2;
                    }else if (id == R.id.sp_type_3) {
                        globalSendType = SendType.Filter_3;
                    }else if (id == R.id.sp_type_4) {
                        globalSendType = SendType.Filter_4;
                    }else if (id == R.id.sp_type_5) {
                        globalSendType = SendType.Filter_5;
                    }else if (id == R.id.sp_type_6) {
                        globalSendType = SendType.Filter_6;
                    }else if (id == R.id.sp_type_7) {
                        globalSendType = SendType.Filter_7;
                    }else if (id == R.id.sp_type_8) {
                        globalSendType = SendType.Filter_8;
                    }else if (id == R.id.sp_type_9) {
                        globalSendType = SendType.Filter_9;
                    }else if (id == R.id.sp_type_10) {
                        globalSendType = SendType.Filter_10;
                    }else if (id == R.id.sp_type_11) {
                        globalSendType = SendType.Filter_11;
                    }else if (id == R.id.sp_type_12) {
                        globalSendType = SendType.Filter_12;
                    }else if (id == R.id.sp_type_13) {
                        globalSendType = SendType.Filter_13;
                    }else if (id == R.id.sp_type_14) {
                        globalSendType = SendType.Filter_14;
                    }else if (id == R.id.sp_type_15) {
                        globalSendType = SendType.Filter_15;
                    }else  {
                        globalSendType = SendType.NoneSend;
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {

                }
            });
        }
    }

    private void initEditText(){
        ed_q_list = new ArrayList<>();
        ed_freq_list = new ArrayList<>();
        ed_gain_list = new ArrayList<>();

        EditText et = findViewById(R.id.edit_Q_1);
        ed_q_list.add(et);
        et = findViewById(R.id.edit_Q_2);
        ed_q_list.add(et);
        et = findViewById(R.id.edit_Q_3);
        ed_q_list.add(et);
        et = findViewById(R.id.edit_Q_4);
        ed_q_list.add(et);
        et = findViewById(R.id.edit_Q_5);
        ed_q_list.add(et);
        et = findViewById(R.id.edit_Q_6);
        ed_q_list.add(et);
        et = findViewById(R.id.edit_Q_7);
        ed_q_list.add(et);
        et = findViewById(R.id.edit_Q_8);
        ed_q_list.add(et);
        et = findViewById(R.id.edit_Q_9);
        ed_q_list.add(et);
        et = findViewById(R.id.edit_Q_10);
        ed_q_list.add(et);
        et = findViewById(R.id.edit_Q_11);
        ed_q_list.add(et);
        et = findViewById(R.id.edit_Q_12);
        ed_q_list.add(et);
        et = findViewById(R.id.edit_Q_13);
        ed_q_list.add(et);
        et = findViewById(R.id.edit_Q_14);
        ed_q_list.add(et);
        et = findViewById(R.id.edit_Q_15);
        ed_q_list.add(et);

        for (int i=0; i<ed_q_list.size(); i++){
            ed_q_list.get(i).setImeOptions(EditorInfo.IME_ACTION_DONE);
            ed_q_list.get(i).setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                    if (i == EditorInfo.IME_ACTION_DONE){
                        int id = textView.getId();
                        if (id == R.id.edit_Q_1) {
                            globalSendType = SendType.Filter_1;
                        } else if (id == R.id.edit_Q_2) {
                            globalSendType = SendType.Filter_2;
                        }else if (id == R.id.edit_Q_3) {
                            globalSendType = SendType.Filter_3;
                        }else if (id == R.id.edit_Q_4) {
                            globalSendType = SendType.Filter_4;
                        }else if (id == R.id.edit_Q_5) {
                            globalSendType = SendType.Filter_5;
                        }else if (id == R.id.edit_Q_6) {
                            globalSendType = SendType.Filter_6;
                        }else if (id == R.id.edit_Q_7) {
                            globalSendType = SendType.Filter_7;
                        }else if (id == R.id.edit_Q_8) {
                            globalSendType = SendType.Filter_8;
                        }else if (id == R.id.edit_Q_9) {
                            globalSendType = SendType.Filter_9;
                        }else if (id == R.id.edit_Q_10) {
                            globalSendType = SendType.Filter_10;
                        }else if (id == R.id.edit_Q_11) {
                            globalSendType = SendType.Filter_11;
                        }else if (id == R.id.edit_Q_12) {
                            globalSendType = SendType.Filter_12;
                        }else if (id == R.id.edit_Q_13) {
                            globalSendType = SendType.Filter_13;
                        }else if (id == R.id.edit_Q_14) {
                            globalSendType = SendType.Filter_14;
                        }else if (id == R.id.edit_Q_15) {
                            globalSendType = SendType.Filter_15;
                        }else  {
                            globalSendType = SendType.NoneSend;
                        }

                    }
                    return true;
                }
            });
            ed_q_list.get(i).setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    if (!b){
                        int id = view.getId();
                        if (id == R.id.edit_Q_1) {
                            globalSendType = SendType.Filter_1;
                        } else if (id == R.id.edit_Q_2) {
                            globalSendType = SendType.Filter_2;
                        }else if (id == R.id.edit_Q_3) {
                            globalSendType = SendType.Filter_3;
                        }else if (id == R.id.edit_Q_4) {
                            globalSendType = SendType.Filter_4;
                        }else if (id == R.id.edit_Q_5) {
                            globalSendType = SendType.Filter_5;
                        }else if (id == R.id.edit_Q_6) {
                            globalSendType = SendType.Filter_6;
                        }else if (id == R.id.edit_Q_7) {
                            globalSendType = SendType.Filter_7;
                        }else if (id == R.id.edit_Q_8) {
                            globalSendType = SendType.Filter_8;
                        }else if (id == R.id.edit_Q_9) {
                            globalSendType = SendType.Filter_9;
                        }else if (id == R.id.edit_Q_10) {
                            globalSendType = SendType.Filter_10;
                        }else if (id == R.id.edit_Q_11) {
                            globalSendType = SendType.Filter_11;
                        }else if (id == R.id.edit_Q_12) {
                            globalSendType = SendType.Filter_12;
                        }else if (id == R.id.edit_Q_13) {
                            globalSendType = SendType.Filter_13;
                        }else if (id == R.id.edit_Q_14) {
                            globalSendType = SendType.Filter_14;
                        }else if (id == R.id.edit_Q_15) {
                            globalSendType = SendType.Filter_15;
                        }else  {
                            globalSendType = SendType.NoneSend;
                        }

                    }

                }
            });
        }


        et = findViewById(R.id.edit_gain_1);
        ed_gain_list.add(et);
        et = findViewById(R.id.edit_gain_2);
        ed_gain_list.add(et);
        et = findViewById(R.id.edit_gain_3);
        ed_gain_list.add(et);
        et = findViewById(R.id.edit_gain_4);
        ed_gain_list.add(et);
        et = findViewById(R.id.edit_gain_5);
        ed_gain_list.add(et);
        et = findViewById(R.id.edit_gain_6);
        ed_gain_list.add(et);
        et = findViewById(R.id.edit_gain_7);
        ed_gain_list.add(et);
        et = findViewById(R.id.edit_gain_8);
        ed_gain_list.add(et);
        et = findViewById(R.id.edit_gain_9);
        ed_gain_list.add(et);
        et = findViewById(R.id.edit_gain_10);
        ed_gain_list.add(et);
        et = findViewById(R.id.edit_gain_11);
        ed_gain_list.add(et);
        et = findViewById(R.id.edit_gain_12);
        ed_gain_list.add(et);
        et = findViewById(R.id.edit_gain_13);
        ed_gain_list.add(et);
        et = findViewById(R.id.edit_gain_14);
        ed_gain_list.add(et);
        et = findViewById(R.id.edit_gain_15);
        ed_gain_list.add(et);


        for (int i=0; i<ed_gain_list.size(); i++){
            ed_gain_list.get(i).setImeOptions(EditorInfo.IME_ACTION_DONE);
            ed_gain_list.get(i).setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                    if (i == EditorInfo.IME_ACTION_DONE){
                        int id = textView.getId();
                        if (id == R.id.edit_gain_1) {
                            globalSendType = SendType.Filter_1;
                        } else if (id == R.id.edit_gain_2) {
                            globalSendType = SendType.Filter_2;
                        }else if (id == R.id.edit_gain_3) {
                            globalSendType = SendType.Filter_3;
                        }else if (id == R.id.edit_gain_4) {
                            globalSendType = SendType.Filter_4;
                        }else if (id == R.id.edit_gain_5) {
                            globalSendType = SendType.Filter_5;
                        }else if (id == R.id.edit_gain_6) {
                            globalSendType = SendType.Filter_6;
                        }else if (id == R.id.edit_gain_7) {
                            globalSendType = SendType.Filter_7;
                        }else if (id == R.id.edit_gain_8) {
                            globalSendType = SendType.Filter_8;
                        }else if (id == R.id.edit_gain_9) {
                            globalSendType = SendType.Filter_9;
                        }else if (id == R.id.edit_gain_10) {
                            globalSendType = SendType.Filter_10;
                        }else if (id == R.id.edit_gain_11) {
                            globalSendType = SendType.Filter_11;
                        }else if (id == R.id.edit_gain_12) {
                            globalSendType = SendType.Filter_12;
                        }else if (id == R.id.edit_gain_13) {
                            globalSendType = SendType.Filter_13;
                        }else if (id == R.id.edit_gain_14) {
                            globalSendType = SendType.Filter_14;
                        }else if (id == R.id.edit_gain_15) {
                            globalSendType = SendType.Filter_15;
                        }else  {
                            globalSendType = SendType.NoneSend;
                        }

                    }
                    return true;
                }
            });
            ed_gain_list.get(i).setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    if (!b){
                        int id = view.getId();
                        if (id == R.id.edit_gain_1) {
                            globalSendType = SendType.Filter_1;
                        } else if (id == R.id.edit_gain_2) {
                            globalSendType = SendType.Filter_2;
                        }else if (id == R.id.edit_gain_3) {
                            globalSendType = SendType.Filter_3;
                        }else if (id == R.id.edit_gain_4) {
                            globalSendType = SendType.Filter_4;
                        }else if (id == R.id.edit_gain_5) {
                            globalSendType = SendType.Filter_5;
                        }else if (id == R.id.edit_gain_6) {
                            globalSendType = SendType.Filter_6;
                        }else if (id == R.id.edit_gain_7) {
                            globalSendType = SendType.Filter_7;
                        }else if (id == R.id.edit_gain_8) {
                            globalSendType = SendType.Filter_8;
                        }else if (id == R.id.edit_gain_9) {
                            globalSendType = SendType.Filter_9;
                        }else if (id == R.id.edit_gain_10) {
                            globalSendType = SendType.Filter_10;
                        }else if (id == R.id.edit_gain_11) {
                            globalSendType = SendType.Filter_11;
                        }else if (id == R.id.edit_gain_12) {
                            globalSendType = SendType.Filter_12;
                        }else if (id == R.id.edit_gain_13) {
                            globalSendType = SendType.Filter_13;
                        }else if (id == R.id.edit_gain_14) {
                            globalSendType = SendType.Filter_14;
                        }else if (id == R.id.edit_gain_15) {
                            globalSendType = SendType.Filter_15;
                        }else  {
                            globalSendType = SendType.NoneSend;
                        }

                    }
                }
            });
        }



        et = findViewById(R.id.edit_freq_1);
        ed_freq_list.add(et);
        et = findViewById(R.id.edit_freq_2);
        ed_freq_list.add(et);
        et = findViewById(R.id.edit_freq_3);
        ed_freq_list.add(et);
        et = findViewById(R.id.edit_freq_4);
        ed_freq_list.add(et);
        et = findViewById(R.id.edit_freq_5);
        ed_freq_list.add(et);
        et = findViewById(R.id.edit_freq_6);
        ed_freq_list.add(et);
        et = findViewById(R.id.edit_freq_7);
        ed_freq_list.add(et);
        et = findViewById(R.id.edit_freq_8);
        ed_freq_list.add(et);
        et = findViewById(R.id.edit_freq_9);
        ed_freq_list.add(et);
        et = findViewById(R.id.edit_freq_10);
        ed_freq_list.add(et);
        et = findViewById(R.id.edit_freq_11);
        ed_freq_list.add(et);
        et = findViewById(R.id.edit_freq_12);
        ed_freq_list.add(et);
        et = findViewById(R.id.edit_freq_13);
        ed_freq_list.add(et);
        et = findViewById(R.id.edit_freq_14);
        ed_freq_list.add(et);
        et = findViewById(R.id.edit_freq_15);
        ed_freq_list.add(et);


        for (int i=0; i<ed_freq_list.size(); i++){
            ed_freq_list.get(i).setImeOptions(EditorInfo.IME_ACTION_DONE);
            ed_freq_list.get(i).setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                    if (i == EditorInfo.IME_ACTION_DONE){
                        int id = textView.getId();
                        if (id == R.id.edit_freq_1) {
                            globalSendType = SendType.Filter_1;
                        } else if (id == R.id.edit_freq_2) {
                            globalSendType = SendType.Filter_2;
                        }else if (id == R.id.edit_freq_3) {
                            globalSendType = SendType.Filter_3;
                        }else if (id == R.id.edit_freq_4) {
                            globalSendType = SendType.Filter_4;
                        }else if (id == R.id.edit_freq_5) {
                            globalSendType = SendType.Filter_5;
                        }else if (id == R.id.edit_freq_6) {
                            globalSendType = SendType.Filter_6;
                        }else if (id == R.id.edit_freq_7) {
                            globalSendType = SendType.Filter_7;
                        }else if (id == R.id.edit_freq_8) {
                            globalSendType = SendType.Filter_8;
                        }else if (id == R.id.edit_freq_9) {
                            globalSendType = SendType.Filter_9;
                        }else if (id == R.id.edit_freq_10) {
                            globalSendType = SendType.Filter_10;
                        }else if (id == R.id.edit_freq_11) {
                            globalSendType = SendType.Filter_11;
                        }else if (id == R.id.edit_freq_12) {
                            globalSendType = SendType.Filter_12;
                        }else if (id == R.id.edit_freq_13) {
                            globalSendType = SendType.Filter_13;
                        }else if (id == R.id.edit_freq_14) {
                            globalSendType = SendType.Filter_14;
                        }else if (id == R.id.edit_freq_15) {
                            globalSendType = SendType.Filter_15;
                        }else  {
                            globalSendType = SendType.NoneSend;
                        }

                    }
                    return true;
                }
            });
            ed_freq_list.get(i).setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    if (!b){
                        int id = view.getId();
                        if (id == R.id.edit_freq_1) {
                            globalSendType = SendType.Filter_1;
                        } else if (id == R.id.edit_freq_2) {
                            globalSendType = SendType.Filter_2;
                        }else if (id == R.id.edit_freq_3) {
                            globalSendType = SendType.Filter_3;
                        }else if (id == R.id.edit_freq_4) {
                            globalSendType = SendType.Filter_4;
                        }else if (id == R.id.edit_freq_5) {
                            globalSendType = SendType.Filter_5;
                        }else if (id == R.id.edit_freq_6) {
                            globalSendType = SendType.Filter_6;
                        }else if (id == R.id.edit_freq_7) {
                            globalSendType = SendType.Filter_7;
                        }else if (id == R.id.edit_freq_8) {
                            globalSendType = SendType.Filter_8;
                        }else if (id == R.id.edit_freq_9) {
                            globalSendType = SendType.Filter_9;
                        }else if (id == R.id.edit_freq_10) {
                            globalSendType = SendType.Filter_10;
                        }else if (id == R.id.edit_freq_11) {
                            globalSendType = SendType.Filter_11;
                        }else if (id == R.id.edit_freq_12) {
                            globalSendType = SendType.Filter_12;
                        }else if (id == R.id.edit_freq_13) {
                            globalSendType = SendType.Filter_13;
                        }else if (id == R.id.edit_freq_14) {
                            globalSendType = SendType.Filter_14;
                        }else if (id == R.id.edit_freq_15) {
                            globalSendType = SendType.Filter_15;
                        }else  {
                            globalSendType = SendType.NoneSend;
                        }

                    }
                }
            });
        }


    }

    private void initCheckBox(){
        cb_bypass_list = new ArrayList<>();
        CheckBox cb = findViewById(R.id.cb_bypass_1);
        cb_bypass_list.add(cb);
        cb = findViewById(R.id.cb_bypass_2);
        cb_bypass_list.add(cb);
        cb = findViewById(R.id.cb_bypass_3);
        cb_bypass_list.add(cb);
        cb = findViewById(R.id.cb_bypass_4);
        cb_bypass_list.add(cb);
        cb = findViewById(R.id.cb_bypass_5);
        cb_bypass_list.add(cb);
        cb = findViewById(R.id.cb_bypass_6);
        cb_bypass_list.add(cb);
        cb = findViewById(R.id.cb_bypass_7);
        cb_bypass_list.add(cb);
        cb = findViewById(R.id.cb_bypass_8);
        cb_bypass_list.add(cb);
        cb = findViewById(R.id.cb_bypass_9);
        cb_bypass_list.add(cb);
        cb = findViewById(R.id.cb_bypass_10);
        cb_bypass_list.add(cb);
        cb = findViewById(R.id.cb_bypass_11);
        cb_bypass_list.add(cb);
        cb = findViewById(R.id.cb_bypass_12);
        cb_bypass_list.add(cb);
        cb = findViewById(R.id.cb_bypass_13);
        cb_bypass_list.add(cb);
        cb = findViewById(R.id.cb_bypass_14);
        cb_bypass_list.add(cb);
        cb = findViewById(R.id.cb_bypass_15);
        cb_bypass_list.add(cb);

        for (int i = 0; i< cb_bypass_list.size(); i++){
            cb_bypass_list.get(i).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    int id = compoundButton.getId();
                    if (id == R.id.cb_bypass_1) {
                        globalSendType = SendType.Filter_1;
                    } else if (id == R.id.cb_bypass_2) {
                        globalSendType = SendType.Filter_2;
                    }else if (id == R.id.cb_bypass_3) {
                        globalSendType = SendType.Filter_3;
                    }else if (id == R.id.cb_bypass_4) {
                        globalSendType = SendType.Filter_4;
                    }else if (id == R.id.cb_bypass_5) {
                        globalSendType = SendType.Filter_5;
                    }else if (id == R.id.cb_bypass_6) {
                        globalSendType = SendType.Filter_6;
                    }else if (id == R.id.cb_bypass_7) {
                        globalSendType = SendType.Filter_7;
                    }else if (id == R.id.cb_bypass_8) {
                        globalSendType = SendType.Filter_8;
                    }else if (id == R.id.cb_bypass_9) {
                        globalSendType = SendType.Filter_9;
                    }else if (id == R.id.cb_bypass_10) {
                        globalSendType = SendType.Filter_10;
                    }else if (id == R.id.cb_bypass_11) {
                        globalSendType = SendType.Filter_11;
                    }else if (id == R.id.cb_bypass_12) {
                        globalSendType = SendType.Filter_12;
                    }else if (id == R.id.cb_bypass_13) {
                        globalSendType = SendType.Filter_13;
                    }else if (id == R.id.cb_bypass_14) {
                        globalSendType = SendType.Filter_14;
                    }else if (id == R.id.cb_bypass_15) {
                        globalSendType = SendType.Filter_15;
                    }else  {
                        globalSendType = SendType.NoneSend;
                    }
                }
            });
        }
    }
    public static char[] floatToBinaryChars(float value) {
        int intBits = Float.floatToIntBits(value);  // 获取float的32位整数表示
        char[] result = new char[4];  // float占4字节，每个字节转为一个char

        for (int i = 0; i < 4; i++) {
            result[i] = (char) ((intBits >> (i * 8)) & 0xFF);  // 按字节提取并转为char
        }

        return result;
    }

    private  void sendAudioPara(){
        byte [] bytes = new byte[40];
        int offset = 0;
        bytes[0] = header_val&0xff;
        bytes[1] = (header_val>>8)&0xff;
        bytes[2] = (header_val>>16)&0xff;
        bytes[3] = (header_val>>24)&0xff;

        offset+=4;
        float f1 = 0f, f2 = 0f, f3=10f, f4 = 0f;
        boolean bypass = false;
        if (globalSendType == SendType.NoneSend) {
            return;

        } else if (globalSendType.ordinal() >= SendType.Filter_1.ordinal()
                    && globalSendType.ordinal() <= SendType.Filter_15.ordinal()) {

            int index = globalSendType.ordinal()-1;
            if (index >= AlgPara.MAX_BANDS)
            {
                return;
            }
            f4 = sp_list.get(index).getSelectedItemPosition()*1.0f;
            try {
                f2 = Float.parseFloat(ed_q_list.get(index).getText().toString());
                f3 = Float.parseFloat(ed_gain_list.get(index).getText().toString());
                f1 = Float.parseFloat(ed_freq_list.get(index).getText().toString());
                bypass = cb_bypass_list.get(index).isChecked();

            } catch (NumberFormatException e) {
                Toast.makeText(MainActivity.this, "输入格式错误:"+e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            //para 1
            bytes[offset++] = 0;
            bytes[offset++] = 0;
            bytes[offset++] = 0;
            bytes[offset++] = 0;

            Spinner sp = findViewById(R.id.sp_channel);
            int speaker_ch = sp.getSelectedItemPosition();
            if (speaker_ch >= MAX_SPEAKERS)
            {
                return;
            }

            //para 2
            bytes[offset++] = (byte)((speaker_ch)&0xff);
            bytes[offset++] = (byte)((speaker_ch>>8)&0xff);
            bytes[offset++] = (byte)((speaker_ch>>16)&0xff);
            bytes[offset++] = (byte)((speaker_ch>>24)&0xff);

            //para 3
            bytes[offset++] = 0;
            bytes[offset++] = 0;
            bytes[offset++] = 0;
            bytes[offset++] = 0;

            //para 4
            if(bypass){
                bytes[offset++] = 1;
                bytes[offset++] = 0;
                bytes[offset++] = 0;
                bytes[offset++] = 0;
            }
            else {
                bytes[offset++] = 0;
                bytes[offset++] = 0;
                bytes[offset++] = 0;
                bytes[offset++] = 0;
            }


            //para 5
            bytes[offset++] = (byte)((index)&0xff);
            bytes[offset++] = (byte)((index>>8)&0xff);
            bytes[offset++] = (byte)((index>>16)&0xff);
            bytes[offset++] = (byte)((index>>24)&0xff);

            //para 6
            int ifreq =  Float.floatToRawIntBits(f1);
            bytes[offset++] = (byte)((ifreq)&0xff);
            bytes[offset++] = (byte)((ifreq>>8)&0xff);
            bytes[offset++] = (byte)((ifreq>>16)&0xff);
            bytes[offset++] = (byte)((ifreq>>24)&0xff);

            //para 7
            int iq =  Float.floatToRawIntBits(f2);
            bytes[offset++] = (byte)((iq)&0xff);
            bytes[offset++] = (byte)((iq>>8)&0xff);
            bytes[offset++] = (byte)((iq>>16)&0xff);
            bytes[offset++] = (byte)((iq>>24)&0xff);

            //para 8
            int igain =  Float.floatToRawIntBits(f3);
            bytes[offset++] = (byte)((igain)&0xff);
            bytes[offset++] = (byte)((igain>>8)&0xff);
            bytes[offset++] = (byte)((igain>>16)&0xff);
            bytes[offset++] = (byte)((igain>>24)&0xff);

            //para 9
            int it =  Float.floatToRawIntBits(f4);
            bytes[offset++] = (byte)((it)&0xff);
            bytes[offset++] = (byte)((it>>8)&0xff);
            bytes[offset++] = (byte)((it>>16)&0xff);
            bytes[offset] = (byte)((it>>24)&0xff);





        } else if (globalSendType == SendType.CHPara) {
            //para 1
            bytes[offset++] = 1;
            bytes[offset++] = 0;
            bytes[offset++] = 0;
            bytes[offset++] = 0;

            Spinner sp = findViewById(R.id.sp_channel);
            int speaker_ch = sp.getSelectedItemPosition();
            if (speaker_ch >= MAX_SPEAKERS)
            {
                return;
            }

            //para 2
            bytes[offset++] = (byte)((speaker_ch)&0xff);
            bytes[offset++] = (byte)((speaker_ch>>8)&0xff);
            bytes[offset++] = (byte)((speaker_ch>>16)&0xff);
            bytes[offset++] = (byte)((speaker_ch>>24)&0xff);

            //para 3
            bytes[offset++] = 0;
            bytes[offset++] = 0;
            bytes[offset++] = 0;
            bytes[offset++] = 0;

            int polarity = cb_ch_polarity.isChecked()?1:0;

            //para 4
            bytes[offset++] = (byte)(polarity&0xff);
            bytes[offset++] = (byte)((polarity>>8) & 0xff);
            bytes[offset++] = (byte)((polarity>>16) & 0xff);
            bytes[offset++] = (byte)((polarity>>24) & 0xff);

            int mute = cb_ch_mute.isChecked()?1:0;
            //para 5
            bytes[offset++] = (byte)((mute)&0xff);
            bytes[offset++] = (byte)((mute>>8)&0xff);
            bytes[offset++] = (byte)((mute>>16)&0xff);
            bytes[offset++] = (byte)((mute>>24)&0xff);

            //para 6
            f1 = Float.parseFloat(ed_ch_gain.getText().toString());
            int igain =  Float.floatToRawIntBits(f1);
            bytes[offset++] = (byte)((igain)&0xff);
            bytes[offset++] = (byte)((igain>>8)&0xff);
            bytes[offset++] = (byte)((igain>>16)&0xff);
            bytes[offset++] = (byte)((igain>>24)&0xff);

            //para 7
            f2 = Float.parseFloat(ed_ch_delay.getText().toString());
            int idelay =  Float.floatToRawIntBits(f2);
            bytes[offset++] = (byte)((idelay)&0xff);
            bytes[offset++] = (byte)((idelay>>8)&0xff);
            bytes[offset++] = (byte)((idelay>>16)&0xff);
            bytes[offset++] = (byte)((idelay>>24)&0xff);

            //para 8
            bytes[offset++] = 0;
            bytes[offset++] = 0;
            bytes[offset++] = 0;
            bytes[offset++] = 0;

            //para 9
            bytes[offset++] = 0;
            bytes[offset++] = 0;
            bytes[offset++] = 0;
            bytes[offset]   = 0;
        }

        if(client != null&&is_connected){

            client.sendData(bytes, ip_addr, IP_PORT);
            globalSendType = SendType.NoneSend;
            showInfoToTextLogView(bytes, ip_addr, IP_PORT, 0);
        }

    }
    private void initView(){
        btn_connect_server = findViewById(R.id.connect);
        btn_connect_server.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){

                    connectToServer();
                    is_connected = true;
                }else{
                    disconnectFromServer();
                    is_connected = false;
                }
            }
        });
        initSpinner();
        initEditText();
        initCheckBox();
        String [] ch_items = new String[]{"FL", "FR", "RL", "RR"};
        //String [] items = new String[]{"0", "1", "2", "3", };
        ArrayAdapter<String> ch_adapter = new ArrayAdapter<>(this, R.layout.custom_spinner_item, ch_items);
        ch_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        Spinner sp = findViewById(R.id.sp_channel);
        sp.setAdapter(ch_adapter);
        sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                saveData(cur_channel);
                cur_channel = position;
                refreshData(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        Button send = findViewById(R.id.btn_send);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               sendAudioPara();
            }
        });
        textBuilder = new SpannableStringBuilder();
        text_log = findViewById(R.id.tv_log);
        text_log.setText(textBuilder);
        text_log.setMovementMethod(new ScrollingMovementMethod());



        ed_ch_gain = findViewById(R.id.et_ch_gain);
        ed_ch_gain.setImeOptions(EditorInfo.IME_ACTION_DONE);
        ed_ch_gain.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId == EditorInfo.IME_ACTION_DONE)
                {
                    float val = Float.parseFloat(v.getText().toString());
                    globalSendType = SendType.CHPara;
                }

                return false;
            }
        });



        ed_ch_delay = findViewById(R.id.et_ch_delay);
        ed_ch_delay.setImeOptions(EditorInfo.IME_ACTION_DONE);
        ed_ch_delay.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId == EditorInfo.IME_ACTION_DONE) {
                    globalSendType = SendType.CHPara;
                }
                return false;
            }
        });
        cb_ch_polarity = findViewById(R.id.cb_ch_polarity);
        cb_ch_polarity.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                globalSendType = SendType.CHPara;
            }
        });
        cb_ch_mute = findViewById(R.id.cb_ch_mute);
        cb_ch_mute.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                globalSendType = SendType.CHPara;
            }
        });

    }
}